﻿Imports System.Activities
Imports System.Activities.Presentation
Imports System.Activities.Statements
Imports System.Activities.Presentation.Metadata
Imports System.Net
Imports Microsoft.VisualBasic.Activities
Imports System.Activities.Expressions
Imports System.Drawing

Public Class WebActivityContainer
    Implements IActivityTemplateFactory, IRegisterMetadata

    Public Function Create(target As System.Windows.DependencyObject) As System.Activities.Activity Implements System.Activities.Presentation.IActivityTemplateFactory.Create
        Dim container As New Sequence With {
            .DisplayName = My.Resources.WebActivityContainerDisplayName}

        Dim cookie As New Variable(Of CookieContainer)("__cookieContainer") With {
            .Default = New VisualBasicValue(Of CookieContainer)("New System.Net.CookieContainer")}
        container.Variables.Add(cookie)

        Dim uaStrings As New Variable(Of String)("__useragent")
        container.Variables.Add(uaStrings)

        Dim childActivity1 As New GetUserAgentStringsActivity With {
            .Result = New VisualBasicReference(Of String)("__useragent")}
        Dim childActivity2 As New WebGetActivity With {
            .CookieContainer = New VisualBasicReference(Of CookieContainer)("__cookieContainer"),
            .UserAgent = New VisualBasicValue(Of String)("__useragent")}

        container.Activities.Add(childActivity1)
        container.Activities.Add(childActivity2)

        Return container
    End Function

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder
        'WebActivityContainer Activity Template
        build.AddCustomAttributes(GetType(WebActivityContainer),
                                              New ToolboxBitmapAttribute(GetType(WebActivityContainer), "WebActivityContainer.png"))

        MetadataStore.AddAttributeTable(build.CreateTable)

    End Sub
End Class
